package com.example.budget3.modify.dialogs;


import com.example.budget3.R;
import com.example.budget3.modify.RecurrTransaction;

import android.app.AlertDialog;
import android.app.Dialog;
import android.content.DialogInterface;
import android.os.Bundle;
import android.support.v4.app.DialogFragment;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ArrayAdapter;
import android.widget.CheckBox;
import android.widget.ListView;

public class DayOfWeekDialogFragment extends DialogFragment {
	
	private ListView dayPicker;
	private CheckBox includeToday;
	private boolean today;
	private String tag;

	@Override
    public Dialog onCreateDialog(Bundle savedInstanceState) {
		
		if(savedInstanceState!=null ){
			today = savedInstanceState.getBoolean("today");
		}else{  
			today = ((RecurrTransaction) getActivity()).getIncludeWeekToday();
		}
		tag = getTag();
		LayoutInflater layoutInflater = getActivity().getLayoutInflater();
		View inflate = layoutInflater.inflate(R.layout.week_dialog, null);
		includeToday = (CheckBox) inflate.findViewById(R.id.checkBox1);
		includeToday.setText("Include This Week");
		includeToday.setChecked(today);
		
		if(tag == "recurrEdit"){
			includeToday.setChecked(false);
			includeToday.setVisibility(View.GONE);
		}
		
		
    	dayPicker = (ListView) inflate.findViewById(R.id.listView1);
    	String[] days = new String[]{"Monday","Tuesday","Wednesday","Thursday","Friday","Saturday", "Sunday"};
    	ArrayAdapter<String> adapter = new ArrayAdapter<String>(getActivity(), R.layout.alert_list, R.id.text1, days);
    	dayPicker.setAdapter(adapter);
    	dayPicker.setClickable(true);
		dayPicker.setOnItemClickListener(new OnItemClickListener() {

			@Override
			public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,
					long arg3) {

                today = includeToday.isChecked();
                ((RecurrTransaction) getActivity()).setDayOfWeek(arg2+2,today);
                dismiss();
				
			}
		});

		AlertDialog.Builder adb = new AlertDialog.Builder( getActivity());
	    adb.setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
            public void onClick(DialogInterface dialog, int id) {}
        });
		

	    AlertDialog dialogBox = adb.setTitle("Pick a Day Of Week")
	    		.setView(inflate).create();
	    dialogBox.show();
		return dialogBox;

    }
	
	
	@Override
	public void onSaveInstanceState(Bundle arg0) {
    	today = includeToday.isChecked();
		arg0.putBoolean("today", today);
		super.onSaveInstanceState(arg0);
	}

}
